<template>
	<view>
		<view style="height: 18upx;"></view>
		<view class="card-view">
			<view class="card-desc">
				<rich-text :nodes="entity.introduction"></rich-text>
			</view>
			<block v-for="(item, index) in authFlag == '20' ? entity.evaluateTopicVOList : entity.evaluateClassifyVOList" :key="index">
				<view class="headline-view">{{item.topicNumber}}、{{item.name}}</view>
				<view class="radio-view">
					<view v-for="(cItem,cIndex) in item.evaluateAnswerVOList" :key="cIndex" class="radio-item"
						@click="onAnswerEvent(item, cItem, index)">
						<image v-if="cItem.active" src="/static/select-icon-sel.png" class="radio-icon"></image>
						<view v-else class="radio-icon"></view>
						<view class="radio-text">{{cItem.options}} . {{cItem.answer}}</view>
					</view>
				</view>
			</block>
		</view>
		<view style="height: 260upx;"></view>
		<FloatView>
			<view style="padding: 0 63upx;">
				<FloatButton @click="onSubmit">提交</FloatButton>
			</view>
		</FloatView>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				authFlag: '10',
				entity: {
					ending: '',
					introduction: '',
					evaluateTopicVOList: [],
					evaluateClassifyVOList: []
				}
			}
		},
		onLoad(options) {
			this.authFlag = options.authFlag
			this.getQuestionnaire()
		},
		methods: {
			async onSubmit() {
				const list = this.authFlag == '20' ? this.entity.evaluateTopicVOList : this.entity.evaluateClassifyVOList
				if (!list.length) return uni.$util.showToast('暂无题目')
				const answerList = []
				for (var i = 0; i < list.length; i++) {
					const entity = list[i]
					const answerArr = entity.evaluateAnswerVOList.filter(item => item.active)
					if (!answerArr.length) return uni.$util.showToast(`请选择 ${entity.topicNumber}、${entity.name}`)
					const answerMapArr = answerArr.map(item => item.answerId)
					answerList.push(...answerMapArr)
				}
				
				uni.$util.showLoading()
				await uni.$api.evaluationSubmission({
					type: this.authFlag,
					answerIdList: answerList
				}).finally(uni.finally)
				await uni.$util.showSuccess()
				uni.navigateBack()
			},
			onAnswerEvent(item, cItem, index) {
				if (item.flag == '10') {
					item.evaluateAnswerVOList.forEach(ftem => ftem.active = false)
					cItem.active = !cItem.active
					return this.$set(this.entity.evaluateTopicVOList, index, item)
				}
				cItem.active = !cItem.active
				this.$set(this.entity.evaluateTopicVOList, index, item)
			},
			async getQuestionnaire() {
				const res = await uni.$api.questionnaire(this.authFlag)
				for (var i = 0; i < res.data.length; i++) {
					res.data[i].evaluateTopicVOList.forEach(item => item.active = false)
					res.data[i].evaluateClassifyVOList.forEach(item => item.active = false)
				}
				this.entity = res.data
			}
		}
	}
</script>

<style scoped lang="scss">
	.card-view {
		width: 707rpx;
		margin: 0 auto;
		background: #FFFFFF;
		border-radius: 14rpx;
		padding: 22upx 29upx;

		.card-desc {
			font-size: 29rpx;
			font-weight: 400;
			color: #2C1101;
		}

		.headline-view {
			font-size: 29rpx;
			font-weight: 600;
			color: #2C1101;
			margin-top: 36upx;
		}

		.title-view {
			font-size: 29rpx;
			font-weight: 400;
			color: #2C1101;
		}

		.radio-view {
			width: 100%;

			.radio-item {
				width: 100%;
				@extend .row-start;
				align-items: flex-start;
				font-size: 29rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #2C1101;
				margin-top: 14upx;

				.radio-icon {
					width: 36upx;
					height: 36upx;
					border-radius: 50%;
					margin-right: 18upx;
					background: #F1E6DF;
				}

				.radio-text {
					width: calc(100% - 54upx);
				}
			}
		}
	}
</style>